package com.example.administrator.social.activitys;

import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.example.administrator.social.R;
import com.example.administrator.social.fragment.ContactFragment;
import com.example.administrator.social.fragment.FoundFragment;
import com.example.administrator.social.fragment.MessageFragment;
import com.example.administrator.social.fragment.MyInfoFragment;
import com.example.administrator.social.util.AllenUtils;
import com.hyphenate.chat.EMClient;
import com.hyphenate.chat.EMOptions;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener,ViewPager.OnPageChangeListener{
    Toolbar mToolbar;
    TextView mTextView;
    ViewPager mViewPager;
    RadioButton mButtonContact,mButtonMessage,mButtonFound,mButtonMyInfo;
    ViewPagerAdapter mViewPagerAdapter;
    RadioGroup mRadioGroup;
    Fragment mContextFragment,mFoundFragment,mMessageFragment,mMyInfoFragment;
    List<Fragment> mFragmentList;

    /**
     * 按钮的没选中显示的图标
     */
    private int[] unselectedIconIds = { R.drawable.main_message,
            R.drawable.main_contact, R.drawable.ic_favorite_outline_24dp,
            R.drawable.main_my_info};
    /**
     * 按钮的选中显示的图标
     */
    private int[] selectedIconIds = { R.drawable.main_message_green,
            R.drawable.main_contact_green, R.drawable.ic_favorite_outline_24dp,
            R.drawable.main_my_info_green };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //沉浸式状态栏
        AllenUtils.setActionBarColor(getWindow(),this,R.color.toolbar_color);
        initFragment();
        mRadioGroup = (RadioGroup) findViewById(R.id.radioGroup);
        mToolbar = (Toolbar) findViewById(R.id.main_toolbar);
        mTextView = (TextView) findViewById(R.id.home_main_top_title);
        mViewPager = (ViewPager) findViewById(R.id.main_view_pager);
        mViewPagerAdapter = new ViewPagerAdapter(getSupportFragmentManager(),mFragmentList);
        mViewPager.setAdapter(mViewPagerAdapter);
        mViewPager.addOnPageChangeListener(this);
        mViewPager.setOffscreenPageLimit(4);
        initBottomButton();
        setSupportActionBar(mToolbar);
        getSupportActionBar().setDisplayShowTitleEnabled(false);
        mRadioGroup.setOnCheckedChangeListener(this);
        initHxSDK();
    }
    private void initHxSDK() {

        /**
         * if have service for third-party
         */
       /* String processAppName = getAppName(pid);
        if (processAppName == null ||!processAppName.equalsIgnoreCase(appContext.getPackageName())) {
            return;
        }*/

        EMOptions options = new EMOptions();
        //Do you need validation when adding friends? default true?
        options.setAcceptInvitationAlways(true);

        //message attachments is upload or download from HX ?
        options.setAutoTransferMessageAttachments(true);
        options.setAutoLogin(true);

        //download the attachments picture
        options.setAutoDownloadThumbnail(true);
        EMClient.getInstance().init(this, options);
        EMClient.getInstance().setDebugMode(true);
    }


    private void initFragment() {
        mFragmentList = new ArrayList<>();
        mContextFragment = new ContactFragment();
        mFoundFragment = new FoundFragment();
        mMessageFragment = new MessageFragment();
        mMyInfoFragment = new MyInfoFragment();
        mFragmentList.add(mMessageFragment);
        mFragmentList.add(mContextFragment);
        mFragmentList.add(mFoundFragment);
        mFragmentList.add(mMyInfoFragment);
    }

    private void initBottomButton() {
        mButtonContact = (RadioButton) findViewById(R.id.navigation_button_contact);
        mButtonMessage = (RadioButton) findViewById(R.id.navigation_button_message);
        mButtonFound = (RadioButton) findViewById(R.id.navigation_button_found);
        mButtonMyInfo = (RadioButton) findViewById(R.id.navigation_button_myinfo);
        mButtonMessage.setTextColor(getResources().getColor(R.color.green));

    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        switch (checkedId) {
            case R.id.navigation_button_message: // 信息选中
                selectPage(0);
                break;
            case R.id.navigation_button_contact: // 联系人选中
                selectPage(1);
                break;
            case R.id.navigation_button_found: // 发现选中
                selectPage(2);
                break;
            case R.id.navigation_button_myinfo: // 个人中心选中
                selectPage(3);
                break;
        }
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        selectPage(position);
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }

    private class ViewPagerAdapter extends FragmentPagerAdapter {
        private List<Fragment> fragmentList = null;

        public ViewPagerAdapter(FragmentManager fm, List<Fragment> fragmentList) {
            super(fm);
            this.fragmentList = fragmentList;
        }

        @Override
        public Fragment getItem(int position) {
            return fragmentList.get(position);
        }

        @Override
        public int getCount() {
            return fragmentList.size();
        }
    }


    private void selectPage(int position) {
        // 将所有的tab的icon变成灰色的
        for (int i = 0; i < mRadioGroup.getChildCount(); i++) {
            Drawable gray = getResources().getDrawable(unselectedIconIds[i]);
            // 不能少，少了不会显示图片
            gray.setBounds(0, 0, gray.getMinimumWidth(),
                    gray.getMinimumHeight());
            RadioButton child = (RadioButton) mRadioGroup.getChildAt(i);
            child.setCompoundDrawables(null, gray, null, null);
            child.setTextColor(getResources().getColor(
                    R.color.dark_gray));
        }
        // 切换页面
        if (mViewPager!=null) mViewPager.setCurrentItem(position, false);
        // 改变图标
        Drawable yellow = getResources().getDrawable(selectedIconIds[position]);
        yellow.setBounds(0, 0, yellow.getMinimumWidth(),
                yellow.getMinimumHeight());
        RadioButton select = (RadioButton) mRadioGroup.getChildAt(position);
        select.setCompoundDrawables(null, yellow, null, null);
        select.setTextColor(getResources().getColor(
                R.color.green));
    }
}
